#include <stdio.h>
#include <string.h>
#include "btree.h"

#define NAMESIZE 32
struct stu_st {
	int id;
	char name[NAMESIZE];
};

static int id_cmp(const void *data, const void *key);
static void show_stu(const void *data);

int main(void)
{
	tree_t *t = NULL;
	struct stu_st s, *p;
	int ids[] = {5, 1, 9, 2, 3, 8, 4, 7, 6, 10};
	int i;
	struct stu_st del_stu;

	tree_init(&t, sizeof(struct stu_st));

	for (i = 0; i < sizeof(ids) / sizeof(*ids); i++) {
		s.id = ids[i];
		snprintf(s.name, NAMESIZE, "stu%d", i + 1);
		tree_insert(t, &s, id_cmp);
	}

	tree_traval_mid(t, show_stu);
	
	printf("**************delete**************\n");
	del_stu.id = 9;
	strcpy(del_stu.name, "stu3");
	p = tree_find(t, &del_stu, id_cmp);
	if (NULL == p) {
		printf("没找到\n");
	} else {
		printf("找到了id:%d, name:%s\n", p->id, p->name);
	}
	tree_delete(t, &del_stu, id_cmp);
	tree_traval_mid(t, show_stu);

	tree_destroy(&t);

	return 0;
}

static int id_cmp(const void *data, const void *key)
{
	struct stu_st *d = (struct stu_st *)data;
	struct stu_st *k = (struct stu_st *)key;

	return d->id - k->id;
}

static void show_stu(const void *data)
{
	struct stu_st *d = (struct stu_st *)data;
	printf("%d %s\n", d->id, d->name);
}

